<?php
/**
 *评论
 */

namespace app\admin\controller;

use app\common\logic\BuildsLogic;
use think\AjaxPage;
use think\Db;
use think\Loader;
use think\Variable;
class Comment  extends Base
{
    public function index(){
        return $this->fetch();
    }

    public function ajax_index(){

        $where = []; // 搜索条件
        $state = input('state/d', 0);
        if ($state > 0) {
            $where['state'] = $state;
        }

        $count = M('Comment')->where($where)->count();
        $Page = new AjaxPage($count, 20);

        $show = $Page->show();
        $orderby1 = input('orderby1/s', '');
        $orderby2 = input('orderby2/s', '');

        $order_str = "$orderby1 $orderby2";
        $comments = M('Comment')->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $b_logic = new BuildsLogic();
        $confs = $b_logic->get_build_config();

        $regions = convert_arr_key($confs['regions'], 'id');

        $this->assign('regions', $regions);
        $this->assign('comments', $comments);

        $this->assign('page', $show);// 赋值分页输出
        return $this->fetch();
    }

    public function edit_comment(){

        $comment_id = input('id/d',0);

        $comment = Db::name('Comment')->where(['id'=>$comment_id])->find();
        $comment_img = Db::name('CommentImg')->where(['comment_id'=>$comment_id])->select();

        $this->assign('comment', $comment);
        $this->assign('commint_img', $comment_img);

        return $this->fetch();
    }

    public function del_comment(){
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);
        $ids = rtrim($ids, ",");

        // 删除
        M("Comment")->whereIn('id', $ids)->delete();  //设施表

        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U("Admin/Comment/index")]);
    }
}